package com.woquyin.controller.api;

import java.util.List;

import com.google.common.collect.Lists;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.ext.interceptor.POST;
import com.woquyin.common.Constance;
import com.woquyin.interceptor.ApiAuthInterceptor;
import com.woquyin.model.BizCompanyPerson;
import com.woquyin.model.BizTemplet;
import com.woquyin.vo.ReturnData;

/**
 * 公司人员相关的接口
 * 
 * @author luyao
 */
@Before(ApiAuthInterceptor.class)
public class PersonalController extends Controller {

	/**
	 * 查询人员列表
	 */
	// TODO 登录状态
	public void list() {
		String companyId = getPara(0);
		int pageNumber = getParaToInt(1, 1);
		// TODO SecUser loginUser = (SecUser)getSessionAttr("userInfo");
		setAttr("data", BizCompanyPerson.dao.query(pageNumber, companyId, "6342d4af17dc492aa5c987c273910716"));
		renderJsp("personalInfoList.jsp");
	}

	/**
	 * 删除单个或多个人员
	 */
	@Before(POST.class)
	public void del() {
		String personalId = getPara("personalId");
		ReturnData<BizTemplet> resp = new ReturnData<>();
		if (personalId.contains("-")) {
			// 批量删除
			List<String> del_ids = Lists.newArrayList();
			// 填充id集合
			for (String string : personalId.split("-")) {
				del_ids.add(string);
			}
			if (del_ids.size() > 1) {
				if (BizCompanyPerson.dao.deleteById(del_ids)) {
					resp.setCode(Constance.WholeStateCodeEnum.SUCCESS.getCode());
					renderJson(resp);
					return;
				}
			}
		} else {
			// 单个删除
			if (BizCompanyPerson.dao.deleteById(personalId)) {
				resp.setCode(Constance.WholeStateCodeEnum.SUCCESS.getCode());
				renderJson(resp);
				return;
			}
		}
		resp.setCode(Constance.WholeStateCodeEnum.OTHER_ERROR.getCode());
		renderJson(resp);
	}

}
